﻿Imports Npgsql
Imports System.Text

Public Class TicketDetail
    Dim intTicketID As Integer
    Dim aTicket As clsTicket

    Public Property getTicketID() As Integer
        Get
            Return intTicketID
        End Get

        Set(ByVal value As Integer)
            intTicketID = value
        End Set
    End Property

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        '
        ' Close the Ticket Detail Information form
        '
        Me.Close()
    End Sub

    Private Sub TicketDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Initialize the ticket based on ticketid
        aTicket = New clsTicket(intTicketID)

        ' Populate the form with the values in the ticket class instance
        txtTicketID.Text = aTicket.TicketID
        txtDateCreated.Text = aTicket.DateCreated.ToString
        txtTicketSubject.Text = aTicket.Subject
        txtTicketSubjectDesc.Text = aTicket.SubjectDescription
        txtTicketPriority.Text = aTicket.PriorityDesc
        txtTicketType.Text = aTicket.IssueTypeDesc
        txtSubmittedBy.Text = aTicket.SubmittedBy

        ' Determine if the ticket is closed based on the dateresolved field
        If aTicket.DateResolved = Nothing Then
            ' Don't allow the ticket to be added to the KB, allow the ticket to be closed, allow notes to be added
            chkKBFlag.Enabled = False
            btnResolveTicket.Enabled = True
            txtTicketNotes.Enabled = True
            cmbxAssignedTo.Enabled = True
        Else
            ' Allow the ticket to be added to the KB, don't allow the ticket to be closed, don't allow notes to be added
            chkKBFlag.Enabled = True
            btnResolveTicket.Enabled = False
            txtDateClosed.Text = aTicket.DateResolved.ToString ' Add the dateresolved to the form
            txtTicketNotes.Enabled = False
            cmbxAssignedTo.Enabled = False
        End If

        ' Determine if the ticket is part of the knowledgebase and update the kb checkbox
        If aTicket.InKnowledgeBase Then
            Me.chkKBFlag.Checked = True
        Else
            Me.chkKBFlag.Checked = False
        End If

        PopulateTechDropDown()

        If aTicket.AssignedId = Nothing Then
            Me.cmbxAssignedTo.SelectedIndex = -1
        Else
            Me.cmbxAssignedTo.SelectedValue = aTicket.AssignedId
        End If

        ' Clear the notes field and then populate it with the string from the ticket class instance
        RefreshExistingNotes()
    End Sub

    Private Sub PopulateTechDropDown()
        Me.cmbxAssignedTo.DisplayMember = "assignedto"
        Me.cmbxAssignedTo.ValueMember = "userid"
        Me.cmbxAssignedTo.DataSource = clsUIBase.AddTechs()
    End Sub

    Private Sub RefreshExistingNotes()
        ' Clear the notes textbox
        txtExistingNotes.Clear()

        ' Populate the notes textbox with the notes from the ticket class instance (uses a StringBuilder)
        txtExistingNotes.Text = aTicket.GetTicketNotes.ToString
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ' If there are notes to be saved, add them to the ticket
        If txtTicketNotes.Text <> "" Then
            aTicket.AddNote(Me.txtTicketNotes.Text)
            RefreshExistingNotes()
            txtTicketNotes.Text = ""
        End If

        ' Determine if the status of the kb checkbox has changed, allow the flag status to be saved to the ticket class
        If chkKBFlag.CheckState <> If(aTicket.InKnowledgeBase, CheckState.Checked, CheckState.Unchecked) Then
            If chkKBFlag.Checked = True Then
                aTicket.AddTicketToKB()
            Else
                aTicket.RemoveTicketFromKB()
            End If
        End If

        ' Determine if the assignedid dropdown differs from the current value int he db, if it is different, update the ticket with the current tech assignment
        If cmbxAssignedTo.SelectedValue <> aTicket.AssignedId Then
            If cmbxAssignedTo.SelectedIndex = -1 Then
                aTicket.UpdateAssignedTech(Nothing)
            Else
                aTicket.UpdateAssignedTech(cmbxAssignedTo.SelectedValue)
            End If
        End If

        ' Disable the save button until another change is made to the ticket
        Me.btnSave.Enabled = False
    End Sub

    Private Sub btnResolveTicket_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResolveTicket.Click
        ' Display a message to ensure the use wants to close the current ticket.
        If MessageBox.Show("Closed tickets can not be reopened. Additionally closing a ticket makes it elligible for the Knowledge Base. Are you sure?", _
                           "Ticket Close Confirmation", _
                           MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            aTicket.ResolveTicket()

            btnResolveTicket.Enabled = False
            chkKBFlag.Enabled = True
            txtDateClosed.Text = aTicket.DateResolved.ToString
        End If
    End Sub

    Private Sub chkKBFlag_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles chkKBFlag.CheckedChanged
        ' Deterine if the current check state of the kb checkbox is different than the value of the flag for the current ticket
        If chkKBFlag.CheckState <> If(aTicket.InKnowledgeBase, CheckState.Checked, CheckState.Unchecked) Then
            Me.btnSave.Enabled = True
        Else
            Me.btnSave.Enabled = False
        End If
    End Sub

    Private Sub txtTicketNotes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTicketNotes.TextChanged
        ' Enable the save button if a note is typed into the note textbox
        If txtTicketNotes.Text = "" Then
            Me.btnSave.Enabled = False
        Else
            Me.btnSave.Enabled = True
        End If
    End Sub

    Private Sub cmbxAssignedTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbxAssignedTo.SelectedIndexChanged
        ' Ensure the tech assignment drop down is populated, if it is check to see if it's value differes from the value in the db, allow save if it differs
        If cmbxAssignedTo.SelectedIndex <> -1 Then
            If CInt(cmbxAssignedTo.SelectedValue.ToString) <> aTicket.AssignedId Then
                Me.btnSave.Enabled = True
            Else
                Me.btnSave.Enabled = False
            End If
        End If
    End Sub
End Class